import urlConfig from './config.js'

import qs from 'qs'

let ApiUrl = urlConfig

export default {
	apiUrl: ApiUrl,
	get(url, needToken, options) {
		let newUrl = url
		if (url.indexOf('http') == -1) {
			newUrl = ApiUrl + url;
		}
		var token = uni.getStorageSync('ele-mix-token')
		let aData = {
			...options
		}
		var browser = uni.getSystemInfoSync().uniPlatform === 'mp-weixin' ? 'wechat' : 'alipay'
		var header1 = {
			"browser": browser
		}
		if (needToken){
			header1.Authorization = token
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: newUrl,
				data: aData,
				dataType: 'json',
				header: header1,
				success(res) {
					resolve(res);
				},
				fail(error) {
					console.log('错误1');
					reject(error);
				}
			})
		});

	},

	post(url, needToken, options) {
		let newUrl = ApiUrl + url;
		var token = uni.getStorageSync('ele-mix-token')
		let aData = {
			...options
		}
		var browser = uni.getSystemInfoSync().uniPlatform === 'mp-weixin' ? 'wechat' : 'alipay'
		var header1 = {
			"browser": browser,
			"content-type": "application/x-www-form-urlencoded"
		}

		if (needToken){
			aData.utoken = token
			header1.Authorization = token
		}
		return new Promise((resolve, reject) => {
			uni.request({
				method: "POST",
				url: newUrl,
				data: aData,
				dataType: 'json',
				header: header1,
				success(res) {
					resolve(res.data);

				},
				fail(error) {
					reject(error);
				}
			})
		});
	},

	postJson(url, needToken, options) {
		let newUrl = ApiUrl + url;
		var token = uni.getStorageSync('ele-mix-token')
		let aData = {
			...options
		}
		aData.utoken = token
		console.log('请求地址', newUrl, aData)
		var browser = uni.getSystemInfoSync().uniPlatform === 'mp-weixin' ? 'wechat' : 'alipay'
		var header1 = {
			"browser": browser,
			"content-type": "application/x-www-form-urlencoded"
		}
		if (needToken){
			header1.Authorization = token
		}
		return new Promise((resolve, reject) => {
			uni.request({
				method: "POST",
				url: newUrl,
				data: aData,
				dataType: 'json',
				formData: aData,
				header: header1,
				success(res) {
					resolve(res);
				},
				fail(error) {
					reject(error);
				}
			})
		});
	},

	toast: function(title, duration = 30000) {
		if (title.length > 10) {
			duration = 3000
		}
		if (title.length > 15) {
			duration = 3500
		}

		uni.showToast({
			title,
			duration,
			icon: 'none'
		});
	},

	load: function(title = '加载中...') {
		uni.showLoading({
			title,
		})
	},
	hideLoad: function() {
		uni.hideLoading()
	},
	noMultipleClicks(methods,info){
		if (this.onClick){
			this.onClick = false
			if (info){
				methods(info)
			}else{
				methods()
			}
			
			setTimeout(res=>{
				this.onClick = true
			},1000)
		}
		
	}

}